<?php
namespace Api\Controller;
use Think\Controller;
class QuanController extends CommonController {
	public function index(){
        if (isset($_GET['pageNum'])) {
            $page = I('pageNum');
            $page = (int)$page * 10;
        } else {
            $page = 0;
        }

		$map['tp_quan.wid'] = session('wid');
        if(!empty($_GET['topic'])){
            $map['tp_quan.topic'] = $_GET['topic'];
        }
        $list = M("Quan")->join('tp_member ON tp_quan.openid = tp_member.openid')->where($map)
                         ->field("tp_quan.*, tp_member.nickname, tp_member.headimgurl, tp_member.openid as follow_openid")
                         ->order("last_time desc")
                         ->limit(0, $page)
                         ->select();
        foreach ($list as $key => $value) {
            $list[$key]['pics'] = explode(',', $value['pics']);

            $eq['tp_quan_reply.wid'] = session('wid');
            $eq['tp_quan_reply.qid'] = $value['id'];
            $list[$key]['reply_list'] = M("QuanReply")->join('tp_member ON tp_quan_reply.openid = tp_member.openid')->where($eq)->field("tp_quan_reply.*, tp_member.nickname, tp_member.headimgurl")->limit(5)->select();
            $eq1['qid'] = $value['id'];
            $eq1['openid'] = $_GET['openid'];
            $list[$key]['is_zan'] = M("QuanZanLog")->where($eq1)->find();
        }
        //顶部轮播图
        $arr['banner'] = M('adv')->where(['type'=>'quan','wid'=>session('wid')])->select();
        $this->ajaxReturn($list);
	}
    public function banner()
    {
        //顶部轮播图
        $arr = M('adv')->where(['type'=>'quan','wid'=>session('wid')])->select();
        $this->ajaxReturn($arr);

    }
    // 获取新闻列表
    public function newsList(){
        $map['wid'] = session('wid');
        $list = M("News")->where($map)->order('last_time desc')->select();
        foreach ($list as $key => $value) {
            $list[$key]['pics'] = explode(',', $value['pics']);
        }
        $this->ajaxReturn($list);
    }
    //获取资讯分类列表
    public function cateList(){
        $map = array();
        $map['status'] = 1;
        $map['wid'] = session('wid');
        $list = M("news_cate")->where($map)->order('add_time desc')->select();
        $this->ajaxReturn($list);
    }
    //搜索资讯列表
    public function  selectNews(){
        $id = $_GET['id'];
        $list = M('news')
            ->where(array('cate_id'=>$id,'wid'=>session('wid')))
            ->select();
        foreach ($list as $key => $value) {
            $list[$key]['pics'] = explode(',', $value['pics']);
            $count = M('news_reply')
                ->where(array('nid'=>$value['id']))
                ->group('openid')
                ->count('id');
            if(empty($count)){
                $count = 0;
            }
            $list[$key]['reply_num'] = $count;
            $list[$key]['add_time'] = $this->getAnnounceTime($value['create_time']);
        }
        $this->ajaxReturn($list);
    }
    //搜索资讯列表
    public function  searchNews(){
        $key = $_GET['key'];
        $map = array();
        $map['title'] = array('like', "%".$key."%");
        $map['wid'] = session('wid');
        $list = M('news')
            ->where(array($map))
            ->select();
        foreach ($list as $key => $value) {
            $list[$key]['pics'] = explode(',', $value['pics']);
        }
        $this->ajaxReturn($list);
    }
    public function newsItem(){
        $map['wid'] = session('wid');
        $map['id'] = $_GET['id'];
        $list = M("News")->where($map)->find();
        if($list['zan_num'] > 0){
            $list['is_zan'] = true;
        }else{
            $list['is_zan'] = false;
        }
        $reply_list = M('news_reply')
            ->alias('n')
            ->join("tp_member m ON m.openid=n.openid","LEFT")
            ->field("n.*,m.nickname, m.headimgurl")
            ->where(array('nid'=>$_GET['id']))
            ->select();
        $list['reply_list'] = $reply_list;
        $this->ajaxReturn($list);
    }
    // 获取视频列表
    public function videoList(){
        $map['wid'] = session('wid');
        $map['status'] = 1;
        $map['start_date'] = array('egt', date("Y-m-d"));
        $list['zhibo_list'] = M("Zhibo")->where($map)->select();
        $eq['wid'] = session('wid');
        $eq1['wid'] = session('wid');
        foreach ($list['zhibo_list'] as $k => $v) {
            $eq['mid'] = $v['member_id'];
            $list['zhibo_list'][$k]['nickname'] = M("Member")->where($eq)->getField('nickname');

            $eq1['id'] = array('in', explode(',', $v['goods']));
            $eq1['type'] = '';
            $goods_list = M("Goods")->where($eq1)->select();
            foreach ($goods_list as $key => $value) {
                $goods_list[$key]['pics'] = explode(',', $value['pics']);
            }
            $list['zhibo_list'][$k]['goods_list'] = $goods_list;
        }

        $map['video_path'] = array('neq', '');
        $list['video_list'] = M("Quan")->where($map)->select();

        $this->ajaxReturn($list);
    }
    // 获取礼物列表
    public function giftList(){
        $map['wid'] = session('wid');
        // $map['wid'] = 0;
        $list = M("QuanGift")->where($map)->select();
        $this->ajaxReturn($list);
    }
    // 生成礼物订单
    public function payGift(){
        $map['wid'] = session('wid');
        $wechat = M("Wechat")->where($map)->find();
        $appid = $wechat['appid'];
        $mch_id = $wechat['mch_id'];
        $key = $wechat['partner_key'];

        $map['wid'] = session('wid');
        $map['id'] = $_POST['gift_id'];
        $info = M("QuanGift")->where($map)->find();
        if(!$info){
            $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }
        $data['wid'] = session('wid');
        $data['type'] = $_POST['type'];
        $data['gift_id'] = $info['id'];
        $data['from'] = $_POST['from'];
        $data['to'] = $_POST['to'];
        $data['out_trade_no'] = $this->build_order_no();
        $data['title'] = $info['name'];
        $data['price'] = $info['price'];
        $data['pay_money'] = $info['price'];
        $data['status'] = 0;
        $data['is_pay'] = 0;
        $data['create_time'] = date("Y-m-d H:i:s");
        if(!M("QuanGiftOrder")->add($data)){
            $arr = array("err_code"=>400,"err_msg"=>"2网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }

        $url = "https://".$_SERVER['HTTP_HOST'].U("Notify/index",array('type'=>'gift'));
        // echo $url;
        vendor('Weixinpay.WxPayJsApiPay');
        $tools = new \JsApiPay();
        $input = new \WxPayUnifiedOrder();
        $input->SetBody("购买礼物");
        $input->SetAttach($data['title']);
        $input->SetOut_trade_no($data['out_trade_no']);
        $input->SetTotal_fee(($data['pay_money']*100));
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        // $input->SetGoods_tag("test3");
        $input->SetNotify_url($url);   //支付回调地址，这里改成你自己的回调地址。
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($_POST['from']);
        $input->SetAppid($appid);
        $input->SetMch_id($mch_id);

        try{
            $wxorder = \WxPayApi::unifiedOrder($input, $key);
        }catch(Exception $e){
            $arr = array("err_code"=>400,"err_msg"=>$e->getMessage());
            $this->ajaxReturn($arr);
        }
        if($wxorder['return_code'] == "FAIL"){
            $arr = array("err_code"=>400,"err_msg"=>$wxorder['return_msg']);
            $this->ajaxReturn($arr);
        }
        try{
            $jsApiParameters = $tools->GetJsApiParameters($wxorder, $key);
        }catch(Exception $e){
            $arr = array("err_code"=>400,"err_msg"=>$e->getMessage());
            $this->ajaxReturn($arr);
        }
        echo $jsApiParameters;
    }
    // 获取热门话题列表
    public function topicList(){
        $map['wid'] = session('wid');
        $map['topic'] = array('neq', '');
        $list = M("Quan")->where($map)->group('topic')->order("last_time desc")->select();
        foreach ($list as $key => $value) {
            $list[$key]['pics'] = explode(',', $value['pics']);
        }
        $this->ajaxReturn($list);
    }
    // 获取达人榜
    public function darenList(){
        $map['tp_quan.wid'] = session('wid');
        if($_GET['type'] = 'today'){
            $map['tp_quan.last_time'] = array('between', array(date("Y-m-d H:i:s", strtotime("-1 days")), date("Y-m-d H:i:s")));
        }
        $list = M("Quan")->join('tp_member ON tp_quan.openid = tp_member.openid')->where($map)->field("tp_quan.*, tp_member.nickname, tp_member.headimgurl, sum(tp_quan.zan_num) as all_zan_num")->group('tp_quan.openid')->order("all_zan_num desc")->select();
        foreach ($list as $key => $value) {
            $list[$key]['pics'] = explode(',', $value['pics']);
        }
        $this->ajaxReturn($list);
    }

    public function member(){
        $map['wid'] = session('wid');
        $map['openid'] = $_GET['openid'];

        $info = M("Member")->where($map)->find();

        $eq['follow_openid'] = $_GET['openid'];
        $info['follow_num'] = M("QuanFollow")->where($eq)->count();

        $info['love_num'] = 0;

        $eq1['to'] = $_GET['openid'];
        $eq1['is_pay'] = 1;
        $info['gift_num'] = M("QuanGiftOrder")->where($eq1)->count();

        $eq2['tp_quan.openid'] = $_GET['openid'];
        $info['quan_list'] = M("Quan")->join('tp_member ON tp_quan.openid = tp_member.openid')->where($eq2)->field("tp_quan.*, tp_member.nickname, tp_member.headimgurl, tp_member.openid as follow_openid")->order("last_time desc")->select();

        $info['pics'] = array();
        foreach ($info['quan_list'] as $key => $value) {
            $pics = explode(',', $value['pics']);
            $info['quan_list'][$key]['pics'] = $pics;
            foreach ($pics as $k => $v) {
                if(!empty($v)){
                   $info['pics'][] = $v; 
                }
            }
        }
        $this->ajaxReturn($info);
    }

	public function item(){
        $map['tp_quan.wid'] = session('wid');
        $map['tp_quan.id'] = $_GET['id'];
        $info = M("Quan")->join('tp_member ON tp_quan.openid = tp_member.openid')->where($map)->field("tp_quan.*, tp_member.nickname, tp_member.headimgurl, tp_member.openid as follow_openid")->find();
        $info['pics'] = explode(",", $info['pics']);

        $eq1['qid'] = $info['id'];
        $eq1['openid'] = $_GET['openid'];
        $info['is_zan'] = M("QuanZanLog")->where($eq1)->getField('id');
        $eq['tp_quan_reply.wid'] = session('wid');
        $eq['tp_quan_reply.qid'] = $_GET['id'];
        $info['reply_list'] = M("QuanReply")->join('tp_member ON tp_quan_reply.openid = tp_member.openid')->where($eq)->field("tp_quan_reply.*, tp_member.nickname, tp_member.headimgurl")->select();
        $eq2['follow_openid'] = $info['follow_openid'];
        $eq2['openid'] = $_GET['openid'];
        $info['is_follow'] = M("QuanFollow")->where($eq2)->getField('id');
        foreach ($info['reply_list'] as $key => $value) {
        	$info['reply_list'][$key]['num'] = $key+1;
        }
        $this->ajaxReturn($info);
    }

	public function add(){
        $data['content'] = I("post.content");
        $data['pics'] = I("post.pics");
        $data['openid'] = I("post.openid");
        $data['video_path'] = I("post.video_path");
        $data['video_thumb'] = I("post.video_thumb");
        $data['wid'] = session('wid');
        $data['create_time'] = date("Y-m-d H:i:s");

        if(empty($data['content']) && empty($data['video_path']) && empty($data['pics'])){
        	$arr = array("err_code"=>400,"err_msg"=>"请填写发帖内容！");
            $this->ajaxReturn($arr);
        }
        if(empty($data['openid'])){
        	$arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }

        if(substr($data['content'], 0, 1) == '#'){
            preg_match("/#(.*?)#/", $data['content'], $match);
            $data['topic'] = $match[1];
        }

        if($info_id = M("Quan")->where($map)->add($data)){
            $map['id'] = $info_id;
            $data['status'] = 1;
            M("Quan")->where($map)->save($data);
            $arr = array("err_code"=>0, "err_msg"=>"发布成功！");
            $this->ajaxReturn($arr);
        }else{
            $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }

    }

    public function reply(){
        $data['content'] = I("post.content");
        // $data['pics'] = I("post.pics");
        $data['openid'] = I("post.openid");
        $data['wid'] = session('wid');
        $data['qid'] = I("post.id");
        $data['create_time'] = date("Y-m-d H:i:s");

        if(empty($data['content'])){
        	$arr = array("err_code"=>400,"err_msg"=>"请输入回帖内容！");
            $this->ajaxReturn($arr);
        }
        if(empty($data['openid'])){
        	$arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }

        if($info_id = M("QuanReply")->where($map)->add($data)){
            $arr = array("err_code"=>0, "err_msg"=>"发布成功！");
            $this->ajaxReturn($arr);
        }else{
            $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }
    }

    public function favo(){
    	$map['wid'] = session('wid');
        $map['id'] = $_GET['id'];
        if(M("Quan")->where($map)->setInc('favo_num')){
        	$arr = array("err_code"=>0, "err_msg"=>"发布成功！");
            $this->ajaxReturn($arr);
        }else{
        	$arr = array("err_code"=>400,"err_msg"=>"你已经关注过了！");
            $this->ajaxReturn($arr);
        }
    }

    // 点赞
    public function zan(){
    	$map['wid'] = session('wid');
        $map['id'] = $_GET['id'];

        $eq['qid'] = $_GET['id'];
        $eq['openid'] = $_GET['openid'];
        if(M("QuanZanLog")->where($eq)->find()){
            M("QuanZanLog")->where($eq)->delete();
            if(M("Quan")->where($map)->setDec('zan_num')){
                $arr = array("err_code"=>0, "err_msg"=>"-1");
                $this->ajaxReturn($arr);
            }else{
                $arr = array("err_code"=>400,"err_msg"=>"网络忙！");
                $this->ajaxReturn($arr);
            }
        }else{
            M("QuanZanLog")->add($eq);
            if(M("Quan")->where($map)->setInc('zan_num')){
                $arr = array("err_code"=>0, "err_msg"=>"1");
                $this->ajaxReturn($arr);
            }else{
                $arr = array("err_code"=>400,"err_msg"=>"网络忙！");
                $this->ajaxReturn($arr);
            }
        }
    }
    // 关注
    public function follow(){
        $model = M('QuanFollow');

        $eq['follow_openid'] = $_POST['follow_openid'];
        $eq['openid'] = $_POST['openid'];
        if($model->where($eq)->find()){
            if($model->where($eq)->delete()){
                $arr = array("err_code"=>0, "err_msg"=>"-1");
                $this->ajaxReturn($arr);
            }else{
                $arr = array("err_code"=>400,"err_msg"=>"网络忙！");
                $this->ajaxReturn($arr);
            }
        }else{
            $eq['create_time'] = date("Y-m-d H:i:s");
            if($model->add($eq)){
                $arr = array("err_code"=>0, "err_msg"=>"1");
                $this->ajaxReturn($arr);
            }else{
                $arr = array("err_code"=>400,"err_msg"=>"网络忙！");
                $this->ajaxReturn($arr);
            }
        }
    }
    public function news_zan(){
        $map['wid'] = session('wid');
        $map['id'] = $_GET['id'];

        $eq['qid'] = $_GET['id'];
        $eq['openid'] = $_GET['openid'];
        if(M("NewsZanLog")->where($eq)->find()){
            M("NewsZanLog")->where($eq)->delete();
            if(M("News")->where($map)->setDec('zan_num')){
                $arr = array("err_code"=>0, "err_msg"=>"-1");
                $this->ajaxReturn($arr);
            }else{
                $arr = array("err_code"=>400,"err_msg"=>"网络忙！");
                $this->ajaxReturn($arr);
            }
        }else{
            M("NewsZanLog")->add($eq);
            if(M("News")->where($map)->setInc('zan_num')){
                $arr = array("err_code"=>0, "err_msg"=>"1");
                $this->ajaxReturn($arr);
            }else{
                $arr = array("err_code"=>400,"err_msg"=>"网络忙！");
                $this->ajaxReturn($arr);
            }
        }
    }
    public function news_reply(){
        $data['content'] = I("post.content");
        // $data['pics'] = I("post.pics");
        $data['openid'] = I("post.openid");
        $data['wid'] = session('wid');
        $data['nid'] = I("post.id");
        $data['reply_openid'] = I('post.reply_openid');
        $data['reply_nickname'] = I('post.reply_nickname');
        $data['create_time'] = date("Y-m-d H:i:s");

        if(empty($data['content'])){
            $arr = array("err_code"=>400,"err_msg"=>"请输入回帖内容！");
            $this->ajaxReturn($arr);
        }
        if(empty($data['openid'])){
            $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }

        if($info_id = M("NewsReply")->where($map)->add($data)){
            $arr = array("err_code"=>0, "err_msg"=>"发布成功！");
            $this->ajaxReturn($arr);
        }else{
            $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }
    }

}